package com.baijia.dao.impl;

import com.baijia.dao.PassportDao;
import com.baijia.dao.po.Passport;
import com.baijia.util.JdbcUtil;

import com.mysql.jdbc.Statement;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

/**
 * Created by bjhl on 16/4/25.
 */
@Repository
public class PassportDaoImpl extends CommonDaoImpl<Passport, Long> implements PassportDao {

    @Resource
    @Qualifier("jdbcTemplate")
    private JdbcTemplate jdbcTemplate;

    private String selectSql =
        "select id,mobile,password,username,email,user_id,org_id,third_id,create_time,update_time from cdb.passport ";

    private static String INSERT_USER =
        "insert into cdb.passport(mobile,password,username,email,user_id,org_id,third_id,create_time,mobile_verify) values(?,?,?,?,?,?,?,?,?)";

    public PassportDaoImpl() {
        super(Passport.class);
    }

    @Override
    public long savePassport(final Passport passport) {
        KeyHolder keyHolder = new GeneratedKeyHolder();
        jdbcTemplate.update(new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
                PreparedStatement ps = con.prepareStatement(INSERT_USER, Statement.RETURN_GENERATED_KEYS);
                int index = 0;
                JdbcUtil.setString(ps, ++index, passport.getMobile());
                JdbcUtil.setString(ps, ++index, passport.getPassword());
                JdbcUtil.setString(ps, ++index, passport.getUsername());
                JdbcUtil.setString(ps, ++index, StringUtils.isBlank(passport.getEmail()) ? null : passport.getEmail());
                JdbcUtil.setLong(ps, ++index, passport.getUserId());
                JdbcUtil.setLong(ps, ++index, passport.getOrgId());
                JdbcUtil.setLong(ps, ++index, passport.getThirdID());
                JdbcUtil.setTimestamp(ps, ++index, new Date());
                JdbcUtil.setInt(ps, ++index, passport.getMobileVerify());
                return ps;
            }
        }, keyHolder);

        long autoIncId = keyHolder.getKey().longValue();
        return autoIncId;
    }

    @Override
    public Passport findByMobile(String mobile) {
        List<Passport> list =
            jdbcTemplate
                .query(
                    "select id,mobile,password,username,email,user_id as userId,org_id as orgId from cdb.passport where mobile = ?",
                    new BeanPropertyRowMapper<>(Passport.class), new Object[] { mobile });
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public int updateUserIdAndOrgId(long id, long userId, long orgId) {
        return jdbcTemplate.update("update cdb.passport set user_id = ?,org_id= ? where id = ?", new Object[] { userId,
            orgId, id });
    }

    @Override
    public int updatePwd(Long passportId, String newPwd) {
        return jdbcTemplate.update("update cdb.passport set password = ? where id = ?", new Object[] { newPwd,
            passportId });
    }

    @Override
    public int updatePwdByMobile(String pwdHash, String mobile) {
        return jdbcTemplate.update("update cdb.passport set password = ? where mobile = ?", new Object[] { pwdHash,
            mobile });
    }

    @Override
    public int updateMobileById(String mobile, Long passportId) {
        return jdbcTemplate.update("update cdb.passport set mobile = ?, mobile_verify=1 where id = ?", new Object[] {
            mobile, passportId });
    }

    @Override
    protected String selectSql() {
        return selectSql;
    }
}
